package com.gorkr.bio.file;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.UUID;

/**
 * @author gorkr
 * @date 2022/05/31 22:30
 **/
public class ServerReaderThread extends Thread {

    private Socket socket;

    public ServerReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            String suffix = dataInputStream.readUTF();
            System.out.println("接收文件类型："+ suffix);

            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\gorkr\\Pictures\\tmp\\server\\" + UUID.randomUUID() + suffix);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = dataInputStream.read(buffer))>0){
                // 在这里阻塞
                fileOutputStream.write(buffer,0,len);
            }
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
    